package valfazz.android.karfinder.activity;

import valfazz.android.karfinder.R;
import valfazz.android.karfinder.util.SongUpdater;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;

public class KarFinderActivity extends TabActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		buildTabs();
		checkDatabaseEmpty();
	}
	
	/**
	 * @author Val
	 * 
	 */
	private void buildTabs() {
		Resources res = getResources();
		TabHost tabHost = getTabHost();
		
		boolean isPortrait = res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
		
		if (!isPortrait) {
			tabHost.getTabWidget().setOrientation(LinearLayout.VERTICAL);
		}
				
		Class<?>[] classNames = { SearchActivity.class,
								  SongActivity.class,
								  FavoriteActivity.class,
								  //PlaylistActivity.class,
								  HelpActivity.class };
		
		String[] specNames = res.getStringArray(R.array.tab_spec_names);
		String[] indicatorNames = res.getStringArray(R.array.tab_indicator_names);
		int[] tabIcons = { R.drawable.ic_tab_search,
						   R.drawable.ic_tab_song, 
						   R.drawable.ic_tab_favorite,
						   //R.drawable.ic_tab_playlist,
						   R.drawable.ic_tab_help };
		
		for (int i = 0; i < specNames.length; i++) {
			tabHost.addTab(
					tabHost.newTabSpec(specNames[i])
							.setIndicator(
									createIndicatorView(
											tabHost,
											indicatorNames[i],
											res.getDrawable(tabIcons[i])))
							.setContent(new Intent().setClass(this, classNames[i])));
		}
	}
	
	/**
	 * @author Val
	 * 
	 */
	private void checkDatabaseEmpty() {
		SongUpdater songUpdater = new SongUpdater(KarFinderActivity.this);
		songUpdater.updateFromFile();
	}
	
	private View createIndicatorView(TabHost tabHost, CharSequence label, Drawable icon) {
	       View tabIndicator = LayoutInflater.from(KarFinderActivity.this).inflate(R.layout.tab_indicator,
	               tabHost.getTabWidget(), // tab widget is the parent
	               false); // no inflate params

	       //final TextView tv = (TextView) tabIndicator.findViewById(R.id.tab_title);
	       //tv.setText(label);
	       //tv.setCompoundDrawablesWithIntrinsicBounds(null,	icon, null, null);

	       final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.tab_icon);
	       iconView.setImageDrawable(icon);

	       return tabIndicator;
	   }
}